package cn.com.lyj6851.modules.user.controller;

import cn.com.lyj6851.logging.aop.AopLog;
import cn.com.lyj6851.modules.shop.domain.YdySystemUserLevel;
import cn.com.lyj6851.modules.user.service.YdySystemUserLevelService;
import cn.com.lyj6851.modules.user.service.dto.YdySystemUserLevelQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "商城:用户等级管理")
@RestController
@RequestMapping("api")
public class SystemUserLevelController {

    private final YdySystemUserLevelService ydySystemUserLevelService;

    public SystemUserLevelController(YdySystemUserLevelService systemUserLevelService) {
        this.ydySystemUserLevelService = systemUserLevelService;
    }

    @AopLog("查询")
    @ApiOperation(value = "查询")
    @GetMapping(value = "/ydySystemUserLevel")
    @PreAuthorize("hasAnyRole('admin','SYSTEMUSERLEVEL_ALL','SYSTEMUSERLEVEL_SELECT')")
    public ResponseEntity getYdySystemUserLevels(YdySystemUserLevelQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(ydySystemUserLevelService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @AopLog("新增")
    @ApiOperation(value = "新增")
    @PostMapping(value = "/ydySystemUserLevel")
    @PreAuthorize("hasAnyRole('admin','SYSTEMUSERLEVEL_ALL','SYSTEMUSERLEVEL_CREATE')")
    public ResponseEntity create(@Validated @RequestBody YdySystemUserLevel resources){
        return new ResponseEntity<>(ydySystemUserLevelService.save(resources),HttpStatus.CREATED);
    }

    @AopLog("修改")
    @ApiOperation(value = "修改")
    @PutMapping(value = "/ydySystemUserLevel")
    @PreAuthorize("hasAnyRole('admin','SYSTEMUSERLEVEL_ALL','SYSTEMUSERLEVEL_EDIT')")
    public ResponseEntity update(@Validated @RequestBody YdySystemUserLevel resources){
        ydySystemUserLevelService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @AopLog("删除")
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/ydySystemUserLevel/{id}")
    @PreAuthorize("hasAnyRole('admin','SYSTEMUSERLEVEL_ALL','SYSTEMUSERLEVEL_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id){
        ydySystemUserLevelService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
